package com.googlecode.bipol.war.entity.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.googlecode.bip.entity.IQuestionValue;
import com.googlecode.bip.entity.ISurveyEntry;
import com.googlecode.bipol.war.entity.ISurvey;

/**
 *
 */
public class ISurveyImpl implements ISurvey
{
    private static final long serialVersionUID = 1L;
    private Timestamp               userDate;
    private List<ISurveyEntry>      surveyEntries = new ArrayList<ISurveyEntry>();
    private List<IQuestionValue>    qvEntries     = new ArrayList<IQuestionValue>();

    /**
     * Returns the date for this {@link ISurveyImpl}
     *
     * @return a normalized {@link Timestamp} - see {@link EntityHelper#getNormalizedTimestamp(java.util.Date)}
     */
    @Override
    public Timestamp getUserDate()
    {
        return this.userDate;
    }

    /**
     * Set the date for this {@link ISurveyImpl}
     *
     * @param date A normalized {@link Timestamp} - see {@link EntityHelper#getNormalizedTimestamp(java.util.Date)}
     */
    @Override
    public void setUserDate( final Timestamp date )
    {
        this.userDate = date;
    }

    /**
     * Returns number of questions in this survey
     *
     * @return The number of questions to be answered
     */
    @Override
    public int getNumberOfQuestions()
    {
        return qvEntries.size();
    }

    /**
     * Returns number of answers already filled in this survey
     *
     * @return The number of answered questions
     */
    @Override
    public int getNumberOfAnswers()
    {
        int count = 0;

        for( IQuestionValue qv : qvEntries ) {
            if( qv.getUserValue() != null ) {
                count++;
                }
            }

        return count;
    }

    @Override
    public List<ISurveyEntry> getSurveyEntries()
    {
        return Collections.unmodifiableList( this.surveyEntries );
    }

    @Override
    public void add( final ISurveyEntry entry )
    {
        this.surveyEntries.add( entry );

        if( entry.isIQuestion() ) {
            this.qvEntries.add( IQuestionValue.class.cast( entry ) );
            }
    }
}
